//
//  MLASavePinPutEntryVC.m
//  MagicLampApp
//
//  Created by Zheng on 2018/12/24.
//  Copyright © 2018 zheng.magic.lamp.app. All rights reserved.
//

#import "MLASavePinPutEntryVC.h"
#import "MLABaseFieldModel.h"
#import "MLABaseFieldView.h"
#import "MLASavePinCateVC.h"
#import "MLASavePinPutInOutModel.h"

@interface MLASavePinPutEntryVC ()

@end

@implementation MLASavePinPutEntryVC {
    NSArray<MLABaseFieldModel *> *modelsArr;
    MLABaseFieldView *cateFieldView; //类型
    MLABaseFieldView *roomFieldView; //库房
    NSString *typeId;
    NSString *roomId;
}



- (void)viewDidLoad {
    [super viewDidLoad];

    typeId = @"";
    roomId = @"";
    
    [self addSubviewsInView];
    
    NSString *title;
    if ([_type isEqualToString:@"1"]) {
        title = @"入库登记";
        
        @weakify(self);
        cateFieldView.clickActionBlock = ^{
            @strongify(self);
            MLASavePinCateVC *cateVC = [[MLASavePinCateVC alloc] init];
            [self.navigationController pushViewController:cateVC animated:YES];
            cateVC.typeSelectBlock = ^(NSString * _Nonnull typeStr, NSString * _Nonnull typeId) {
                self->cateFieldView.inputText = typeStr;
                self->typeId = typeId;
            };
        };
        
        roomFieldView.clickActionBlock = ^{
            @strongify(self);
            MLASavePinCateVC *cateVC = [[MLASavePinCateVC alloc] init];
            cateVC.type = @"3";
            [self.navigationController pushViewController:cateVC animated:YES];
            cateVC.typeSelectBlock = ^(NSString * _Nonnull typeStr, NSString * _Nonnull typeId) {
                self->roomFieldView.inputText = typeStr;
                self->roomId = typeId;
            };
        };

    } else {
        title = @"出库登记";
    }
    [self configTitle:title];
    
}

#pragma mark - LoadData
- (void)loadDataWithParams:(NSDictionary *)params {
    
    NSString *api;
    if ([_type isEqualToString:@"1"]) {
        api = kPutInApply;
    } else {
        api = kPutOutApply;
    }
    
    [MLAHttpManage postHostType:MLAHttpHostType_Jinxiaocun api:api params:params success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        if (self.applySuccessBlock) {
            self.applySuccessBlock();
        }
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^{
        
    }];
}
#pragma mark - Actions
- (void)commitBtnAction {
    CX_Log(@"出入库登记");
    
    NSString *name = [self getContentWithTag:13001];
    NSString *num = [self getContentWithTag:13002];
    NSString *buyer = [self getContentWithTag:13004];
    NSString *checker = [self getContentWithTag:13005];

    if (!typeId) {
        [MLAHudManage showMessage:@"请选择类型"];
        return;
    }
    
    if (name.length == 0) {
        [MLAHudManage showMessage:@"请输入名称"];
        return;
    }
    
    if (num.length == 0) {
        [MLAHudManage showMessage:@"请输入数量"];
        return;
    }
    
    if (!roomId) {
        [MLAHudManage showMessage:@"请选择库房"];
        return;
    }
    
    if (buyer.length == 0) {
        [MLAHudManage showMessage:@"请输入采购员"];
        return;
    }
    
    if (checker.length == 0) {
        [MLAHudManage showMessage:@"请输入核验人"];
        return;
    }
    NSDictionary *params;
    if ([_type isEqualToString:@"1"]) {
        params = @{@"type_id":typeId,
                   @"name":name,
                   @"num":num,
                   @"warehouse_id":roomId,
                   @"apply_name":buyer,
                   @"audit_name":checker};
    } else {
        params = @{@"repertory_id":_model.modelId,
                   @"sum":num,
                   @"apply_name":buyer,
                   @"audit_name":checker};
    }
    
    [self loadDataWithParams:params];

    
}


- (NSString *)getContentWithTag:(NSInteger)tag {
    MLABaseFieldView *fieldView = (MLABaseFieldView *)[self.tableView.tableHeaderView viewWithTag:tag];
    return fieldView.inputText;
}

#pragma mark - Subviews
- (void)addSubviewsInView {
    
    modelsArr = [self getModelsArr];
    
    CGFloat height = 650;
    
    UIView *tableHeaderView = [[UIView alloc] init];
    tableHeaderView.backgroundColor = CX_GetColorHex(0xFCFCFC, 1);
    tableHeaderView.frame = CGRectMake(0, 0, CX_GetScreenWidth(), height);
    
    [modelsArr enumerateObjectsUsingBlock:^(MLABaseFieldModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        MLABaseFieldView *fieldView = [[MLABaseFieldView alloc] init];
        fieldView.tag = 13000+idx;
        fieldView.model = obj;
        [tableHeaderView addSubview:fieldView];
        
        if (idx == 0) {
            self->cateFieldView = fieldView;
            [fieldView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(20);
                make.left.right.equalTo(tableHeaderView);
                make.height.mas_equalTo(85);
            }];
        } else {
            if (idx == 3) {
                self->roomFieldView = fieldView;
            }
            [fieldView mas_makeConstraints:^(MASConstraintMaker *make) {
                UIView *view = [tableHeaderView viewWithTag:13000+idx-1];
                make.left.right.height.equalTo(view);
                make.top.equalTo(view.mas_bottom);
            }];
        }
    }];
    
    UIButton *commitBtn = [self getCommitButtonWithTitle:@"登记"];
    [commitBtn addTarget:self action:@selector(commitBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [tableHeaderView addSubview:commitBtn];
    [commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        UIView *view = [tableHeaderView viewWithTag:13000+self->modelsArr.count-1];
        make.top.equalTo(view.mas_bottom).offset(10);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(40);
    }];
    
    self.tableView.tableHeaderView = tableHeaderView;
    [tableHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(0);
        make.width.mas_equalTo(CX_GetScreenWidth());
        make.height.mas_equalTo(height);
    }];
    
}


- (NSArray<MLABaseFieldModel *> *)getModelsArr {
    
    NSArray *infosArr;
    if (_model) {
        infosArr = @[CX_GetFieldInfoDic(@"类别：", @"请选择类别", @"30", @"0", @"0", @"0", _model.type_name),
                     CX_GetFieldInfoDic(@"名称：", @"输入名称", @"30", @"0", @"0", @"0", _model.name),
                     CX_GetFieldInfoDic(@"数量：", @"输入数量", @"30", @"0", @"1", @"0", @""),
                     CX_GetFieldInfoDic(@"库房：", @"请选择库房", @"30", @"0", @"0", @"0", _model.warehouse_name),
                     CX_GetFieldInfoDic(@"采购员：", @"输入采购员", @"30", @"0", @"1", @"0", @""),
                     CX_GetFieldInfoDic(@"核验人：", @"输入核验人", @"30", @"0", @"1", @"0", @"")];

    } else {
        infosArr = @[CX_GetFieldInfoDic(@"类别：", @"请选择类别", @"30", @"0", @"0", @"1", @""),
                     CX_GetFieldInfoDic(@"名称：", @"输入名称", @"30", @"0", @"1", @"0", @""),
                     CX_GetFieldInfoDic(@"数量：", @"输入数量", @"30", @"0", @"1", @"0", @""),
                     CX_GetFieldInfoDic(@"库房：", @"请选择库房", @"30", @"0", @"0", @"1", @""),
                     CX_GetFieldInfoDic(@"采购员：", @"输入采购员", @"30", @"0", @"1", @"0", @""),
                     CX_GetFieldInfoDic(@"核验人：", @"输入核验人", @"30", @"0", @"1", @"0", @"")];
    }
    
    return [[MLABaseFieldModel mj_objectArrayWithKeyValuesArray:infosArr] copy];
}

@end
